package com.ctesi.service.zhaobiao.hunan;

import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import com.ctesi.utils.StringExtract;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//对长沙招标页面进行爬取
@Service
public class ChangShaProcessorService implements PageProcessor {

    private Site site;
    @Override
    public void process(Page page) {
        Random random = new Random();

        // 生成5000到10000之间的随机整数
        int randomTime = random.nextInt(10000 - 5000 + 1) + 5000;

        //设置请求头和等待时间
        this.site = Site.me()
                .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
                .setTimeOut(randomTime) // 设置超时时间为5秒到10秒之间的数
                .setRetryTimes(5); // 设置重试次数为 3 次
        site.addHeader("Connection", "keep-alive");




        //1. 判断是裂变url还是内容url
        if (page.getUrl().get().contains("index")) {
            //裂变url执行对应逻辑
            for (int i = 1; i <= 10; i++) {
                String currentUrl = page.getUrl().get();
                String bidTime = page.getHtml().xpath(String.format("/html/body/div[4]/div[4]/div/div[3]/div[2]/ul/li[%d]/p[2]/text()", i)).toString();
                String listUrl = "https://fwpt.csggzy.cn" + page.getHtml().xpath(String.format("/html/body/div[4]/div[4]/div/div[3]/div[2]/ul/li[%d]/p[1]/a/@href",i)).toString();
                if (bidTime.contains("2023")) {
                    page.addTargetRequest(listUrl);
                    System.out.println("添加url:" + listUrl + "成功");
                } else {
                    System.out.println("此裂变url不是2023年的，舍去。");
                }
            }

        } else if (!page.getUrl().get().contains("index")) {
            //内容url执行对应逻辑
            System.out.println("----------------------开始爬取页面内容-------------------");
            //招标主体body
            String bidContextTags = page.getHtml().xpath("/html/body/div[4]/div[4]/div/div[2]/div[1]/div").get();
            String bidContext = StringExtract.removeNbsp(StringExtract.removeTags(bidContextTags));
            System.out.println(bidContext);

            //获取项目名称
            String bidTitle = page.getHtml().xpath("/html/body/div[4]/div[4]/div/div[2]/div[1]/table/tbody/tr[1]/td[2]/text()").toString();
            page.putField("bidTitle", bidTitle);
            System.out.println("项目名称：" + bidTitle);

            //招标编号
            String bidId = page.getHtml().xpath("/html/body/div[4]/div[4]/div/div[2]/div[1]/table/tbody/tr[2]/td[2]/text()").toString();
            page.putField("bidId", bidId);
            System.out.println("招标编号：" + bidId);

            //招标类型
            if (page.getUrl().get().contains("jyxxfjzbgg")) {
                page.putField("bidType", "房建市政");
            } else if (page.getUrl().get().contains("jyxxslzbgg")) {
                page.putField("bidType", "水利工程");
            } else if (page.getUrl().get().contains("jyxxjtzbgg")) {
                page.putField("bidType", "交通工程");
            }

            //招标省份
            page.putField("bidProvince", "湖南省长沙市");

            //招标范围
            String pattern1 = "招标范围\\n(.*?).*?投标人资格要求"; // 匹配要提取内容的正则表达式
            Pattern rangePattern = Pattern.compile(pattern1, Pattern.DOTALL);
            Matcher matcher2 = rangePattern.matcher(bidContext);
            if (matcher2.find()) {
                String bidArange = matcher2.group(1);
                System.out.println("招标范围：" + bidArange);
            }

            //招标发布时间
            String bidPubTime = page.getHtml().xpath("/html/body/div[4]/div[4]/div/div[2]/div[1]/p[2]/span[1]/text()").get();
            page.putField("bidPubTime", bidPubTime);
            System.out.println("招标发布时间" + bidPubTime);

            //备注:长沙市没有该字段。

            //数据创建时间
            LocalDateTime createTime = LocalDateTime.now();
            page.putField("createTime", createTime);
            System.out.println("数据创建时间" + createTime);

            //数据更新时间
            LocalDateTime updateTime = LocalDateTime.now();
            page.putField("updateTime", updateTime);
            System.out.println("数据更新时间" + updateTime);

            //招标公告url
            String bidUrl = page.getUrl().get();
            page.putField("bidUrl", bidUrl);
            System.out.println("当前url:" + bidUrl);

            //招标所属管辖地区
            String bidArea = page.getHtml().xpath("/html/body/div[4]/div[4]/div/div[2]/div[1]/table/tbody/tr[3]/td[4]/text()").toString();
            page.putField("bidArea", bidArea);
            System.out.println("招标所属管辖地区:" + bidArea);

            //投标人资格要求
            String patternCredit = "(\\d+)\\..*?资格要求(.*?)\\n.*?(\\1-1)\\."; // 匹配要提取内容的正则表达式
            Pattern regexPatternCredit = Pattern.compile(patternCredit, Pattern.DOTALL);
            Matcher matcherCredit = regexPatternCredit.matcher(bidContext);
            if (matcherCredit.find()) {
                String bidCredit = matcherCredit.group(1);
                page.putField("bidCredit", bidCredit);
                System.out.println("投标人资格要求：" + bidCredit);
            }


            //评标办法
            String pattern = "评标办法.*?采用(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPattern = Pattern.compile(pattern, Pattern.DOTALL);
            Matcher matcher = regexPattern.matcher(bidContext);
            if (matcher.find()) {
                String bidJudgeMethod = matcher.group(1);
                page.putField("bidJudgeMethod", bidJudgeMethod);
                System.out.println("评标办法：" + bidJudgeMethod);
            }

            //招标状态:长沙市没有该字段

            //招标单位信息

            //招标单位
            String patternbidUnity = "招 标 人：.*?(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternbidUnity = Pattern.compile(patternbidUnity, Pattern.DOTALL);
            Matcher matcherpatternbidUnity = regexPatternbidUnity.matcher(bidContext);
            if (matcherpatternbidUnity.find()) {
                String bidUnity = matcherpatternbidUnity.group(1);
                page.putField("bidUnity", bidUnity);
                System.out.println("招标单位：" + bidUnity);
            }

            //招标联系人
            String patternbidName = "招 标 人：.*?联 系 人：(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternbidName = Pattern.compile(patternbidName, Pattern.DOTALL);
            Matcher matcherpatternbidName = regexPatternbidName.matcher(bidContext);
            if (matcherpatternbidName.find()) {
                String bidName = matcherpatternbidName.group(1);
                page.putField("bidName", bidName);
                System.out.println("招标联系人：" + bidName);
            }

            //招标联系人电话
            String patternbidPhone = "招 标 人：.*?电 话：(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternbidPhone = Pattern.compile(patternbidPhone, Pattern.DOTALL);
            Matcher matcherpatternbidPhone = regexPatternbidPhone.matcher(bidContext);
            if (matcherpatternbidPhone.find()) {
                String bidPhone = matcherpatternbidPhone.group(1);
                page.putField("bidPhone", bidPhone);
                System.out.println("招标联系人电话：" + bidPhone);
            }

            //招标地址
            String patternbidAddress = "招 标 人：.*?地 址：(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternbidAddress = Pattern.compile(patternbidAddress, Pattern.DOTALL);
            Matcher matcherpatternbidAddress = regexPatternbidAddress.matcher(bidContext);
            if (matcherpatternbidAddress.find()) {
                String bidAddress = matcherpatternbidAddress.group(1);
                page.putField("bidAddress", bidAddress);
                System.out.println("招标地址：" + bidAddress);
            }

            //代理机构信息

            //代理机构
            String patternagencyUnity = "招标代理机构：(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternagencyUnity = Pattern.compile(patternagencyUnity, Pattern.DOTALL);
            Matcher matcherpatternagencyUnity = regexPatternagencyUnity.matcher(bidContext);
            if (matcherpatternagencyUnity.find()) {
                String agencyUnity = matcherpatternagencyUnity.group(1);
                page.putField("agencyUnity", agencyUnity);
                System.out.println("代理机构：" + agencyUnity);
            }

            //代理联系人姓名
            String patternagencyName = "招标代理机构：.*?联 系 人：(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternagencyName = Pattern.compile(patternagencyName, Pattern.DOTALL);
            Matcher matcherpatternagencyName = regexPatternagencyName.matcher(bidContext);
            if (matcherpatternagencyName.find()) {
                String agencyName = matcherpatternagencyName.group(1);
                page.putField("agencyName", agencyName);
                System.out.println("代理机构联系人：" + agencyName);
            }

            //代理联系人电话
            String patternagencyPhone = "招标代理机构：.*?电 话：(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternagencyPhone = Pattern.compile(patternagencyPhone, Pattern.DOTALL);
            Matcher matcherpatternagencyPhone = regexPatternagencyPhone.matcher(bidContext);
            if (matcherpatternagencyPhone.find()) {
                String agencyPhone = matcherpatternagencyPhone.group(1);
                page.putField("agencyPhone", agencyPhone);
                System.out.println("代理机构联系人电话：" + agencyPhone);
            }

            //代理机构地址
            String patternagencyAddress = "招标代理机构：.*?地 址：(.*?)\\n"; // 匹配要提取内容的正则表达式
            Pattern regexPatternagencyAddress = Pattern.compile(patternagencyAddress, Pattern.DOTALL);
            Matcher matcherpatternagencyAddress = regexPatternagencyAddress.matcher(bidContext);
            if (matcherpatternagencyAddress.find()) {
                String agencyAddress = matcherpatternagencyAddress.group(1);
                page.putField("agencyAddress", agencyAddress);
                System.out.println("代理机构地址：" + agencyAddress);
            }

            try {
                Thread.sleep(randomTime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    public Site getSite() {
        return PageProcessor.super.getSite();
    }
}
